import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import users from '../components/user/Users.vue'
Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login'
    }, {
        path: '/login',
        name: 'login',
        component: login
    },
    {
        path: '/home',
        name: 'home',
        component: home,
        redirect: '/welcome',
        children: [{
                path: '/welcome',
                name: 'welcome',
                component: welcome,
            },
            {
                path: '/users',
                name: 'users',
                component: users,
            }
        ]
    }
]

const router = new VueRouter({
    routes
})


//挂载路由  导航守卫控制访问

router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        return next();
    }
    const tokenstr = window.sessionStorage.getItem('token');
    if (!tokenstr) {
        return next('/login');
    }
    next();
})

export default router